/**
 * @file       KeyboardThread.cpp
 * @brief      TODO
 * @author     Huabo Zheng
 * @date       Jan 20, 2012
 * @Copyright  2012, www.netgear.com Inc. All rights reserved.
 */

#include "KeyboardThread.h"
#include "lcd/LCDFactory.h"
#include "lcd/IInput.h"
#include "errorcode.h"
#include "typedefine.h"
#include "EventQueue.h"
#include "IEvent.h"
#include "KeyboardEvent.h"
#include <stdlib.h>
#include <sstream>

namespace NS_LCD_AFW
{

CKeyboardThread::CKeyboardThread() :
		m_bRun(true)
{
	this->setAutoDelete(false);
}

CKeyboardThread::~CKeyboardThread()
{
}

void CKeyboardThread::run()
{
	IInput *pInput = AfwGetLCDFactory()->createInputDevice(0);
	if (NULL == pInput)
	{
		//goto ERR_PROC;
		return;
	}

	char keyCode = -1;
	IEvent *pEvent = NULL;
	while (m_bRun)
	{
		int iRet = pInput->readChar(keyCode);

		switch (iRet)
		{
		case ERR_SUCCESS:
		{
			pEvent = pInput->translateCodeToEvent(keyCode);
		}
			break;
		case ERR_WAIT_TIMEOUT:
		{
			//OBJ_NEW(CIdleEvent,pEvent);
		}
			break;
		case ERR_INPUT_DEV_RTN_FAILED:
		{
			break;
		}
		default:
			//unknow code
			break;
		}

		if (NULL != pEvent)
		{
			//pEvent->setParam(keyCode);
			AfwGetSysEventQueue()->addEvent(pEvent);
			pEvent = NULL;
		}
	}
}

int CKeyboardThread::stop()
{
	m_bRun = false;
	this->join();
	return ERR_SUCCESS;
}

} /* namespace NS_LCD_AFW */
